﻿
using Bootstrap5.Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;

namespace Bootstrap5;

/// <summary>
/// Represents the item of slide in <see cref="Carousel"/> component.
/// </summary>
[CssClass("carousel-item")]
public class CarouselItem: BootstrapChildContentWithChildCascadingComponentBase<CarouselItem, Carousel>
{
    [Parameter] public string ImageSrc { get; set; }
    [Parameter] [CssClass("active")] public bool Active { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Parent.AddItem(this);
    }

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, GetHtmlTagName());
        BuildCommonAttributes(builder);
        builder.AddContent(10,BuildItem);
        builder.CloseElement();
    }

    internal void BuildItem(RenderTreeBuilder builder)
    {
        builder.OpenComponent<Image>(0);
        builder.AddAttribute(1,"src", ImageSrc);
        builder.AddAttribute(2, nameof(Image.CssClass), Css.Display(DisplayType.Block).Width(LengthPercent.Is100));
        builder.CloseComponent();

        if (ChildContent is not null)
        {
            builder.CreateElement(10, "div", ChildContent, new { @class = "carousel-caption d-none d-md-block" });
        }
    }
}
